﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using jnxc.Model;

/// <summary>
///PageBase 的摘要说明
/// </summary>
public class PageBase : System.Web.UI.Page
{
    string virtualPath = jnxc.Common.ConfigHelper.GetConfigString("VirtualPath");
    public jnxc.Model.userinfo user = new jnxc.Model.userinfo();
    public jnxc.BLL.SysUser bllUser = new jnxc.BLL.SysUser();
    public string strUserFun = "";
    public jnxc.BLL.SysLog bllLog = new jnxc.BLL.SysLog();
    public PageBase()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Load += new System.EventHandler(PageBase_Load);
        this.Error += new System.EventHandler(PageBase_Error);
    }
    //错误处理
    protected void PageBase_Error(object sender, System.EventArgs e)
    {
        string errMsg;
        Exception currentError = Server.GetLastError();
        errMsg = "<link rel=\"stylesheet\" href=\"/style.css\">";
        errMsg += "<h1>系统错误：</h1><hr/>系统发生错误， " +
            "该信息已被系统记录，请稍后重试或与管理员联系。<br/>" +
            "错误地址： " + Request.Url.ToString() + "<br/>" +
            "错误信息： <font class=\"ErrorMessage\">" + currentError.Message.ToString() + "</font><hr/>" +
            "<b>Stack Trace:</b><br/>" + currentError.ToString();
        WriteLog("系统发生错误，错误地址：" + Request.Url + "错误信息：" + currentError.Message);
        Response.Write(errMsg);
        Server.ClearError();

    }
    private void PageBase_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["UserInfo"] != null)
            {
                user = Session["UserInfo"] as jnxc.Model.userinfo;
            }
            else
            {
                if (Request.Cookies["UserInfo"] != null)
                {
                    HttpCookie userCookie = Request.Cookies["UserInfo"];
                    user.uid = Convert.ToInt32(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["UserID"]));
                    //user.AddTime = Convert.ToDateTime(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["AddTime"]));
                    //user.AddUserID = Convert.ToInt32(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["AddUserID"]));
                    //user.DeptID = Convert.ToInt32(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["DeptID"]));
                    user.utruename = Convert.ToString(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["DisplayName"]));
                    user.uname = Convert.ToString(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["LoginName"]));
                    user.upwd = Convert.ToString(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["Password"]));
                    //user.RegisterUserID = Convert.ToInt32(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["RegisterUserID"]));
                    user.remark = Convert.ToString(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["Remark"]));
                    //user.ulevel = Convert.ToString(jnxc.Common.DEncrypt.DESEncrypt.Decrypt(userCookie["State"]));
                    Session["UserInfo"] = user;
                }
                else
                {
                    Response.Redirect("/admin/login.aspx", true);
                }
            }
            strUserFun = bllUser.GetFun(Request.RawUrl, this.user.uid);//获取功能权限（实时）
        }

    }
    /// <summary>
    /// 验证功能权限
    /// </summary>
    /// <param name="funCode"></param>
    /// <returns></returns>
    public bool CheckFun(string funCode)
    {
        return this.strUserFun.IndexOf(funCode + ",") >= 0;
    }
    /// <summary>
    /// 输入日志
    /// </summary>
    /// <param name="Message"></param>
    public void WriteLog(string Message)
    {
        SysLog modelLog = new SysLog();
        modelLog.AddTime = DateTime.Now;
        modelLog.AddUserID = 2;
        modelLog.IP = Request.UserHostAddress;
        modelLog.Message = Message;
        modelLog.Page = Request.RawUrl;
        bllLog.Add(modelLog);
    }
}